/* eslint-disable */
interface ConfirmOptions {
  text: string;
  cancelWords?: string;
  sureWords?: string;
  cancelFn?: () => void;
  sureFn?: () => void;
}
interface ToastType {
  hide: (id: string, callback?: any) => void;
  hideAll: () => void;
  showConfirm: (obj: ConfirmOptions) => void;
  showTips: (text: string, callback?: any) => void;
  showToast: (text: string, time?: number, callback?: any) => void;
  showLoading: () => string;
  hideLoading: (id: string) => void;
}
const toast = function (this: any) {
  let _self: ToastType = this;
  const append = (html: any, id: any) => {
    let parent = document.getElementsByTagName("body")[0];
    if (!parent) {
      return;
    }
    let txtNode = document.createElement("div");
    txtNode.className = "yx-toast-panel";
    txtNode.id = id;
    txtNode.innerHTML = html;
    parent.appendChild(txtNode);
  };
  this.hide = (id: string, callback: any) => {
    let body = document.getElementById("toast-body" + id);
    let toast = document.getElementById("yx-toast" + id);
    body && (body.style.transform = "scale3d(0,0,1)");
    toast && (toast.style.opacity = "0");
    setTimeout(() => {
      let node: any = document.getElementById(id);
      if (!node) {
        return;
      }
      node.parentNode.removeChild(node);
      if (callback) {
        callback();
      }
    }, 500);
  };
  this.hideAll = () => {
    let getElementsClass = (classnames: any) => {
      let classobj = []; // 定义数组
      let classint = 0; // 定义数组的下标
      let tags = document.getElementsByTagName("*"); // 获取HTML的所有标签
      let myreg = new RegExp("(\\s|^)" + classnames + "(\\s|$)");
      for (let i in tags) {
        // 对标签进行遍历
        let one: any = tags[i];
        if (one.nodeType === 1) {
          // 判断节点类型
          if (myreg.test(one.getAttribute("class"))) {
            classobj[classint] = tags[i];
            classint++;
          }
        }
      }
      return classobj; // 返回组成的数组
    };
    let nodeArr = getElementsClass("yx-toast-panel");
    let len = nodeArr.length;
    for (let i = 0; i < len; i++) {
      let node = nodeArr[i];
      if (node && node.parentNode) {
        node.parentNode.removeChild(node);
      }
    }
  };
  this.showConfirm = (obj: ConfirmOptions) => {
    _self.hideAll();
    let config = {
      cancelWords: obj.cancelWords ? obj.cancelWords : "取消",
      sureWords: obj.sureWords ? obj.sureWords : "确认",
    };
    let idNum = new Date().getTime() + "-" + Math.ceil(Math.random() * 1000);
    let html =
      '<div class="yx-toast" id="yx-toast' +
      idNum +
      '"><div class="toast-body toast-confirm" id="toast-body' +
      idNum +
      '"><div class="toast-content">' +
      obj.text +
      '</div><div><span class="toast-btn toast-cancel" id="toast-cancel' +
      idNum +
      '">' +
      config.cancelWords +
      '</span><span class="toast-btn toast-sure" id="toast-sure' +
      idNum +
      '">' +
      config.sureWords +
      "</span></div></div></div>";
    append(html, idNum);

    (document.getElementById("toast-cancel" + idNum) as any).onclick = () => {
      _self.hide(idNum, obj.cancelFn);
    };
    let sure: any = document.getElementById("toast-sure" + idNum);
    sure.onclick = () => {
      _self.hide(idNum, obj.sureFn);
    };
  };
  this.showTips = (text: string, callback: any) => {
    _self.hideAll();
    let idNum = new Date().getTime() + "-" + Math.ceil(Math.random() * 1000);
    let html =
      '<div class="yx-toast" id="yx-toast' +
      idNum +
      '"><div class="toast-body toast-tips" id="toast-body' +
      idNum +
      '"><div class="toast-content">' +
      text +
      '</div><div><span class="toast-btn toast-sure" id="toast-sure' +
      idNum +
      '">确认</span></div></div></div>';
    append(html, idNum);
    (document.getElementById("toast-sure" + idNum) as any).onclick = () => {
      _self.hide(idNum, callback);
    };
  };
  this.showToast = (text: string, time?: number, callback?: any) => {
    _self.hideAll();
    if (!time) {
      time = 1500;
    }
    let idNum = new Date().getTime() + "-" + Math.ceil(Math.random() * 1000);
    let html =
      '<div class="yx-toast" id="yx-toast' +
      idNum +
      '"><div class="toast-body toast-box" id="toast-body' +
      idNum +
      '"><div>' +
      text +
      "</div></div></div>";
    append(html, idNum);

    let animation = setTimeout(() => {
      _self.hide(idNum, callback);
    }, time);
    (document.getElementById("yx-toast" + idNum) as any).onclick = () => {
      _self.hide(idNum, callback);
      clearTimeout(animation);
    };
  };
  this.showLoading = (): string => {
    //_self.hideAll();
    const s = require('../../assets/load.gif')
    let idNum = new Date().getTime() + "-" + Math.ceil(Math.random() * 1000);
    let html =
      '<div class="yx-toast loading-toast" id="yx-toast' +
      idNum +
      `"><div class="toast-body toast-loading"><div class="toast-content"><img src="${s}" alt=""/></div></div></div>`;
    append(html, idNum);
    return idNum;
  };
  this.hideLoading = (id: string) => {
    let node: any = document.getElementById(id);
    if (!node) {
      return;
    }
    node.parentNode.removeChild(node);
  };
};
export default new (toast as any)() as ToastType;
